<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="评论内容" prop="content">
          <el-input
            v-model="queryParams.content"
            placeholder="请输入"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="评论人" prop="userId">
          <el-select v-model="queryParams.userId" placeholder="请选择" clearable>
            <el-option
              v-for="(item, index) in userOptions"
              :key="index"
              :value="item.userId"
              :label="item.nickName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="博客标题" prop="blogTitle">
          <el-input
            v-model="queryParams.blogTitle"
            placeholder="请输入"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            :disabled="multiple"
            @click="handleReply"
            v-hasPermi="['blog:comment:reply']"
          >回复</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['blog:comment:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
          >展开/折叠</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="queryCommentList"></right-toolbar>
      </el-row>
      <vxe-table
        :loading="loading"
        resizable
        row-key
        ref="tableRef"
        :row-config="{isHover: true}"
        v-if="refreshTable"
        :data="commentList"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full'"
        :tree-config="{transform: true, rowField: 'id', parentField: 'toCommentId', line: true, expandAll: isExpandAll}"
        :checkbox-config="{checkStrictly: true}"
      >
        <vxe-column
          type="checkbox"
          width="100"
          fixed="left"
          align="left"
          tree-node
        />
        <vxe-column
          title="序号"
          align="left"
          type="seq"
          min-width="80"
        />
        <vxe-column
          title="评论内容"
          header-align="center"
          align="left"
          field="content"
          min-width="350"
          :show-overflow="true"
          type="html"
        >
        </vxe-column>
        <vxe-column
          title="博客标题"
          align="center"
          min-width="280"
          field="blogTitle"
          :show-overflow="true"
        />
        <vxe-column
          title="评论人"
          align="center"
          field="nickName"
          min-width="160"
          :show-overflow="true"
        />
        <vxe-column
          title="被评论人"
          align="center"
          field="toNickName"
          min-width="150"
          :show-overflow="true"
        />
        <vxe-column title="创建时间" align="center" field="createTime" min-width="150">
          <template slot-scope="scope">
            <span>{{
                parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
              }}</span>
          </template>
        </vxe-column>
        <vxe-column title="操作" align="center" fixed="right" min-width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="toRead(scope.row)"
              v-hasPermi="['blog:comment:query']"
            >查看评论
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['blog:comment:remove']"
            >删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="queryCommentList"
      />
    </el-card>

    <!-- 阅读弹窗 -->
    <el-dialog
      title="查看评论"
      :visible.sync="readDialogVisible"
      v-if="readDialogVisible"
      :destroy-on-close="true"
      v-drag-zoom
    >
      <div class="read-container" v-html="form.content"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 回复对话框对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" v-drag-zoom :before-close="cancel" append-to-body>
      <div class="reply-label">
        <el-divider direction="vertical"></el-divider>
        <span>回复的评论</span>
      </div>
      <el-table :data="replyCommentList" auto-resize resizeable border class="table-reply">
        <el-table-column label="评论人" align="center" prop="nickName" width="150" :show-overflow-tooltip="true"/>
        <el-table-column label="博客标题" align="center" prop="blogTitle"  width="250" :show-overflow-tooltip="true"/>
        <el-table-column label="评论内容" align="center" prop="content" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <p v-html="scope.row.content"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="reply-label">
        <el-divider direction="vertical"></el-divider>
        <span>回复的内容</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="form-reply">
        <el-row>
          <el-col :span="24">
            <el-form-item label="评论编号" prop="id" v-show="false">
              <el-input v-model="form.id" :disabled="true" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回复内容">
              <editor-comment v-model="form.content" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>

import { delComment, listComment, insertByBatch } from '@/api/blog/comment'
import { queryUserSelect } from '@/api/system/user'
import store from '@/store'
import Prismjs from 'prismjs'

export default {
  name: 'comment.vue',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论列表数据
      commentList: [],
      // 用户列表
      userOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 控制阅读弹窗
      readDialogVisible: false,
      // 是否展开，默认全展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
        blogTitle: '',
        content: '',
        userId: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "评论编号不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "评论内容不能为空", trigger: "blur" }
        ]
      },
      /** 回复列表 */
      replyCommentList: []
    }
  },
  created() {
    this.getUserList();
    this.queryCommentList();
  },
  methods: {
    /** 查询评论列表 */
    queryCommentList() {
      this.loading = true;
      listComment(this.queryParams).then(res => {
        this.commentList = res.rows;
        this.total = res.total;
        this.loading = false;
        this.$refs.tableRef.reloadData(this.commentList);
      })
    },
    /** 获取用户列表 */
    getUserList() {
      queryUserSelect([]).then(res => {
        this.userOptions = res.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.readDialogVisible = false
      this.reset();
      this.replyCommentList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        content: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryCommentList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.records.map(item => item.id)
      this.single = row.records.length !== 1
      this.multiple = !row.records.length
      this.replyCommentList = [...row.records]
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.id || this.ids
      this.$modal.confirm(`是否确认删除id为${commentIds}的评论？`).then(function() {
        return delComment(commentIds);
      }).then(() => {
        this.queryCommentList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 回复弹框 */
    handleReply() {
      this.form.id = this.ids.join(',');
      this.open = true;
      this.title = "回复";
    },
    /** 阅读 */
    toRead(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.readDialogVisible = true
      this.$nextTick(() => {
        Prismjs.highlightAll()
      })
    },
    /** 展开/折叠 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      })
    },
    /** 提交 */
    submitForm() {
      let replyList = [];
      for (const item of this.replyCommentList) {
        const data = {
          userId: store.getters.userId,
          toUserId: item.userId,
          content: this.form.content,
          toCommentId: item.id,
          toBlogId: item.toBlogId
        };
        replyList.push(data);
      }
      insertByBatch(replyList).then(res => {
        this.$modal.msgSuccess(res.msg);
        this.cancel();
        this.queryCommentList();
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.table-reply{
  margin-bottom: 20px;
}

.reply-label {
  margin-bottom: 20px;
  line-height: 15px;
  height: 15px;
  font-size: 15px;
  font-weight: bold;
  .el-divider--vertical {
    width: 2px !important;
    height: 20px;
  }
}
::v-deep .form-reply {
  .el-form-item__label {
    width: 0 !important;
    display: none !important;
  }
  .el-form-item__content {
    margin-left: 0 !important;
  }
}
</style>
